import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class Reflect {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        //获取类对象的三种方式
        //调用对象的getClass方法
        Books books = new Books();
        Class<? extends Books> c1 = books.getClass();

        //调用类的Class属性
        Class<Books> c2 = Books.class;

        //使用Class类的forName()静态方法
        Class<?> c3 = Class.forName("Books");


//        System.out.println(c1 == c2);
//        System.out.println(c2 == c3);
//        System.out.println(c1 == c3);
        Books books1 = (Books) c1.newInstance();
        System.out.println(books1);

        // 获取一个 public 修饰的指定构造器 通过参数列表指定 含有一个 String 类型参数的构造器
        Constructor<? extends Books> constructor = c1.getConstructor(String.class);
        constructor.setAccessible(true);
        Books books2 = constructor.newInstance("蛋仔派对");
        System.out.println(books2);
        System.out.println(books2.getId());
        Constructor<? extends Books> constructor1 =c2.getConstructor(int.class,String.class,double.class,String.class);
        Books books4 = constructor1.newInstance(2, "wuuw", 62.3, "诡秘之主");
        System.out.println(books4);

        // 获取多个 public 修饰的构造器
        Constructor<?>[] constructors = c2.getConstructors();
        System.out.println(constructors.length);

        // 获取任意修饰符修饰的指定构造器
        Constructor<Books> declaredConstructor = c2.getDeclaredConstructor(int.class, String.class);
        // 通过构造器创建对象 设置不考虑访问修饰符
        declaredConstructor.setAccessible(true);
        Books books3 = declaredConstructor.newInstance(1, "我的26岁女房客");
        System.out.println(books3);

        // 获取任意修饰符修饰的所有构造器
        Constructor<?>[] declaredConstructors = c3.getDeclaredConstructors();
        System.out.println(declaredConstructors.length);
    }
}